package com.hung.service.serviceImpl;

import com.hung.dao.MatterDao;
import com.hung.dao.daoImpl.PageDaoImpl;
import com.hung.entity.PageBean;
import com.hung.entity.Report;
import com.hung.pojo.Matter;
import com.hung.service.MatterService;
import com.hung.util.orm.sqlsession.SqlSession;
import com.hung.util.orm.sqlsession.defaults.DefaultSqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hung
 */
public class MatterServiceImpl implements MatterService {
    /**
     * 使用工厂创建sqlSession对象
     */
    SqlSession sqlSession = new DefaultSqlSession();
    /**
     * 使用SqlSession创建Dao接口的代理对象
     */
    MatterDao matterDao = (MatterDao) sqlSession.getMapper(MatterDao.class);
    PageDaoImpl pageDao = new PageDaoImpl();

    /**
     * 根据页数查询对应数据
     *
     * @param _currentPage 当前页面
     * @param _rows        条数
     * @param condition    条件
     * @return pageBean集合
     */
    @Override
    public PageBean<Matter> findDataOnPage(String _currentPage, String _rows, String condition, Integer pid) {
        //健壮性判断
        if (_currentPage == null || "".equals(_currentPage)) {
            _currentPage = "1";
        }
        if (_rows == null || "".equals(_rows)) {
            _rows = "10";
        }

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //防止按上一页按钮会出错
        if (currentPage <= 0) {
            currentPage = 1;
        }

        //创建对象设置参数
        PageBean<Matter> pb = new PageBean<>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //调用dao查询总记录数
        int totalCount = pageDao.queryTotalCount(condition);
        pb.setTotalCount(totalCount);

        //计算开始页码
        int start = (currentPage - 1) * rows;
        List<Matter> list = pageDao.queryMatters(pid, condition, start, rows);
        pb.setList(list);

        //计算总页码
        int totalPage = (totalCount % rows) == 0 ? totalCount / rows : totalCount / rows + 1;
        pb.setTotalPage(totalPage);

        return pb;
    }

    /**
     * 根据mid获取事件详细信息
     *
     * @param mid
     * @return
     */
    @Override
    public Matter queryMatterById(Integer mid) {
        return matterDao.queryMatterById(mid);
    }

    /**
     * 添加事件
     *
     * @param matter
     * @return
     */
    @Override
    public boolean addMatter(Matter matter) {
        return matterDao.addMatter(matter) > 0;
    }

    /**
     * 根据uid和rid查看是否点赞
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean isPraise(Integer uid, Integer mid) {
        if (matterDao.isPraise(uid, mid) != null && matterDao.isPraise(uid, mid) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据uid和rid查看是否收藏
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean isCollected(Integer uid, Integer mid) {
        if (matterDao.isCollected(uid, mid) != null && matterDao.isCollected(uid, mid) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 点赞
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean praise(Integer uid, Integer mid) {
        return matterDao.praise(uid, mid) > 0 && matterDao.addPraise(mid) > 0;
    }

    /**
     * 收藏
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean favorite(Integer uid, Integer mid) {
        return matterDao.favorite(uid, mid) > 0;
    }

    /**
     * 取消
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean cancelPraise(Integer uid, Integer mid) {
        return matterDao.cancelPraise(uid, mid) > 0 && matterDao.reducePraise(mid) > 0;
    }

    /**
     * 取消
     *
     * @param uid
     * @param mid
     * @return
     */
    @Override
    public boolean cancelFavorite(Integer uid, Integer mid) {
        return matterDao.cancelFavorite(uid, mid) > 0;
    }

    /**
     * 举报
     *
     * @param mid
     * @return
     */
    @Override
    public boolean report(Integer mid, Integer pid) {
        return matterDao.report(mid, pid) > 0;
    }

    /**
     * 查询所有举报
     *
     * @return
     */
    @Override
    public List<Report> queryReports() {
        return matterDao.queryAllReports();
    }

    /**
     * 根据mid删除事件
     *
     * @param mid
     * @return
     */
    @Override
    public boolean deleteMatterById(Integer mid) {
        return matterDao.deleteMatterById(mid) > 0;
    }

    /**
     * 根据uid查询赞过的事件
     *
     * @param uid
     * @return
     */
    @Override
    public List<Matter> queryPraiseById(Integer uid) {
        List<Matter> praises = new ArrayList<>();
        List<Integer> list = matterDao.queryAllPraiseById(uid);
        for (Integer mid : list) {
            Matter matter = matterDao.queryMatterById(mid);
            praises.add(matter);
        }
        return praises;
    }

    /**
     * 根据uid查询收藏过的事件
     *
     * @param uid
     * @return
     */
    @Override
    public List<Matter> queryFavoriteById(Integer uid) {
        List<Matter> favorites = new ArrayList<>();
        List<Integer> list = matterDao.queryAllFavoriteById(uid);
        for (Integer mid : list) {
            Matter matter = matterDao.queryMatterById(mid);
            favorites.add(matter);
        }
        return favorites;
    }

    /**
     * 删除举报
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteReport(Integer id) {
        return matterDao.deleteReport(id)>0;
    }


}
